function X_peaks = find_peaks(X,Rp1=0.01,Rp2=2.3e-3)

%Any number is smaller than eps is considered to be zero
eps1 = max(abs(X))*Rp1;
eps2 = max(abs(X))*Rp2;

%First pass: find all diff(X,1)==0
X_pass1 = find_zeros(X,1,eps1);

%Second pass: find all abs(diff(X,2))<eps2
X_pass2 = find_zeros(X,2,eps2);

%Find only 'sharp' peaks
X_peaks = (X_pass1>X_pass2)';

%Change pulses to impulses
X_peaks = diff(X_peaks)>0;

%Debug mode: draw graph if no output is specified
if(!nargout)
t=([0:length(X)-1]/550)';
t_selected = t(X_peaks);
X_selected = X(X_peaks);
plot(t,X,';signal;',t_selected,X_selected,'x;peaks;','markersize',15),axis([30 35])
endif

endfunction

function X_zeros = find_zeros(X,N=1,eps1)
len = length(X);
diff_X = diff(X,N);
X_zeros((N+1):len) = abs(diff_X) < eps1;

endfunction
